package com.ticket.sass.admin.business.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.FilmBusiness;
import com.ticket.sass.admin.dto.output.film.FilmVersionOutput;
import com.ticket.sass.admin.dto.input.film.FilmListInput;
import com.ticket.sass.admin.dto.output.film.FilmListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.FilmLikeServiceImpl;
import com.ticket.sass.admin.service.impl.FilmServiceImpl;
import com.ticket.sass.admin.service.impl.FilmVersionServiceImpl;
import com.ticket.sass.admin.service.impl.OrderFilmSaleMapServiceImpl;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.entity.tenant.FilmEntity;
import lombok.AllArgsConstructor;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2022-02-28 15:10
 */

@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class FilmBusinessImpl implements FilmBusiness {
    final ModelUtil modelUtil;
    final FilmVersionServiceImpl filmVersionService;
    final FilmServiceImpl filmService;
    final FilmLikeServiceImpl filmLikeService;
    
    final OrderFilmSaleMapServiceImpl orderFilmSaleMapService;
    
    @Override
    public PageModel<FilmListOutput> list(FilmListInput input) {
        IPage<FilmEntity> filmEntityIPage = filmService.iPage(input);
        
        // 转换影片列表对象为输出对象
        List<FilmListOutput> outputs = modelUtil.listToList(filmEntityIPage.getRecords(), new TypeToken<List<FilmListOutput>>(){}.getType());
        
        Map<Long, List<FilmVersionOutput>> filmVersionsByFilmIds = filmVersionService.getFilmVersionsByFilmIds(outputs.stream().map(FilmListOutput::getId).collect(Collectors.toList()));
        
        List<Long> filmIds = outputs.stream().map(FilmListOutput::getId).collect(Collectors.toList());
        
        Map<Long, BigDecimal> totalAmountByFilmIds = orderFilmSaleMapService.getTotalAmountByFilmIds(filmIds);
        
        Map<Long, Integer> totalSaleInfoByFilmIds = orderFilmSaleMapService.getTotalSaleInfoByFilmIds(filmIds);
        
        Map<Long, Integer> countByFilmIds = filmLikeService.getCountByFilmIds(filmIds);
        
        outputs.forEach(o -> {
            o.setSaleTicketAmount(totalAmountByFilmIds.getOrDefault(o.getId(), new BigDecimal("0.0")));
            o.setSaleTicketNum(totalSaleInfoByFilmIds.getOrDefault(o.getId(), 0));
            o.setVersions(filmVersionsByFilmIds.getOrDefault(o.getId(), new ArrayList<>()));
            o.setLikeCount(countByFilmIds.getOrDefault(o.getId(), 0));
        });
        
        // 返回分页模型对象，假设总记录数为影片列表的大小
        return new PageModel<>(filmEntityIPage.getTotal(), outputs);
    }

    @Override
    public void syncFilm() {
    
    }
}
